import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let arr = []
      let detail = Taro.getStorageSync('HouseDetail')
      arr.push(detail)
      action.commonChange([
        { paths: 'main.currentHouse', value: arr },
      ]);
      this.findHouseUsers(detail.houseId)
    },
    /**
    * 查询房屋具体用户信息
    */
    async findHouseUsers(houseId) {
      let res = await api.houseController.findByHouseId({ houseId });
      if (res.code == '200') {
        let tempMembers = res.data;
        let owner = []; //业主
        let other = []; //其他身份
        if (res.data.length > 0) {
          tempMembers.map((item, index) => {
            if (item.houseHoldType == 1) {
              owner.push(item);
            } else {
              other.push(item);
            }
          });
          action.commonChange([
            { paths: 'main.owner', value: owner },
            { paths: 'main.other', value: other },
          ]);
        }
      }
    },
    /**
     * 切换房屋
     */
    async switchHouse(id) {
      let userInfo = (Taro.getStorageSync('b2b-wechat@login')).user;
      let params = {
        id,
        communityHouse: 2, //切换小区1，切换房屋2
      }
      let res = await api.houseController.updateDefHouse(params);
      if (res.success) {
        this.getDefHouseInfo(userInfo.id);
        Taro.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        });
      }
    },
    /**
     * 更新用户信息
     */
    async getDefHouseInfo(userId) {
      let userInfo = Taro.getStorageSync('b2b-wechat@login');
      const params = {
        userId
      };
      let res = await api.pagesIndexController.findDefHouseByUserId(params);
      if (res.success && res.data) {
        let obj = {
          code: '',
          id: '',
          name: ''
        };
        obj.code = res.data.communityCode;
        obj.id = res.data.communityId;
        obj.name = res.data.communityName;
        userInfo.user.communityCode = res.data.communityCode;
        userInfo.user.communityId = res.data.communityId;
        userInfo.user.communityName = res.data.communityName;
        userInfo.user.houseId = res.data.houseId;
        userInfo.user.status = res.data.status;
        Taro.setStorageSync('selectDistance', obj);
        Taro.setStorageSync('b2b-wechat@login', userInfo);
        setTimeout(() => {
          Taro.switchTab({ url: '/pages/index/index' });
        }, 1000);
      }
    },
    /**
     * 审核住户申请
     */
    async houseProve(id, status) {
      let userInfo = (Taro.getStorageSync('b2b-wechat@login')).user;
      const params = {
        id,
        status,
        userId: userInfo.id,
      };
      let res = await api.houseController.houseProve(params);
      if (res.success) {
        let detail = Taro.getStorageSync('HouseDetail')
        this.findHouseUsers(detail.houseId)
        action.commonChange('main.showModel', false);
        Taro.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        });
      }
    }
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageAHouseDetailMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
